﻿Function.prototype.bind = function (object) {
    var _method = this;
    return function () {
        _method.apply(object, arguments);
    }
};
String.prototype.nocache = function () {
    var str = this;
    if (this.indexOf("?") == -1) {
        str = str + "?rnd=" + new Date().getTime();
    } else if (this.indexOf("&") == -1) {
        str = str + "&rnd=" + new Date().getTime();
    }
    return str;
};
/**
	 * 扩展String方法
	 */
	$.extend(String.prototype, {
		isPositiveInteger:function(){
			return (new RegExp(/^[1-9]\d*$/).test(this));
		},
		isInteger:function(){
			return (new RegExp(/^\d+$/).test(this));
		},
		isNumber: function(value, element) {
			return (new RegExp(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/).test(this));
		},
		trim:function(){
			return this.replace(/(^\s*)|(\s*$)|\r|\n/g, "");
		},
		startsWith:function (pattern){
			return this.indexOf(pattern) === 0;
		},
		endsWith:function(pattern) {
			var d = this.length - pattern.length;
			return d >= 0 && this.lastIndexOf(pattern) === d;
		},
		replaceSuffix:function(index){
			return this.replace(/\[[0-9]+\]/,'['+index+']').replace('#index#',index);
		},
		trans:function(){
			return this.replace(/&lt;/g, '<').replace(/&gt;/g,'>').replace(/&quot;/g, '"');
		},
		encodeTXT: function(){
			return (this).replaceAll('&', '&amp;').replaceAll("<","&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;");
		},
		replaceAll:function(os, ns){
			return this.replace(new RegExp(os,"gm"),ns);
		},
		replaceTm:function($data){
			if (!$data) return this;
			return this.replace(RegExp("([{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)])","g"), function($1){
				return $data[$1.replace(/[{}\\(\\)]+/g, "")];
			});
		},
		replaceTmById:function(_box){
			var $parent = _box || $(document);
			return this.replace(RegExp("([{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)])","g"), function($1){
			    var $pk= $parent.find("input:checkbox[rel="+$1.replace(/[{}\\(\\)]+/g, "")+"]:checked:eq(0)");
			    if($pk.size()>0) {
			        return $pk.val();
			    }
				var $input = $parent.find("#"+$1.replace(/[{}\\(\\)]+/g, ""));
				return $input.val() ? $input.val() : $1;
			});
		},
		isFinishedTm:function(){
			return !(new RegExp("[{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)]").test(this)); 
		},
		skipChar:function(ch) {
			if (!this || this.length===0) {return '';}
			if (this.charAt(0)===ch) {return this.substring(1).skipChar(ch);}
			return this;
		},
		isValidPwd:function() {
			return (new RegExp(/^([_]|[a-zA-Z0-9]){6,32}$/).test(this)); 
		},
		isValidMail:function(){
			return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(this.trim()));
		},
		isSpaces:function() {
			for(var i=0; i<this.length; i+=1) {
				var ch = this.charAt(i);
				if (ch!=' '&& ch!="\n" && ch!="\t" && ch!="\r") {return false;}
			}
			return true;
		},
		isPhone:function() {
			return (new RegExp(/(^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/).test(this));
		},
		isUrl:function(){
			return (new RegExp(/^[a-zA-z]+:\/\/([a-zA-Z0-9\-\.]+)([-\w .\/?%&=:]*)$/).test(this));
		},
		isExternalUrl:function(){
			return this.isUrl() && this.indexOf("://"+document.domain) == -1;
		}
	});

 function List() {
	this.a = [];
	this.push = function (s) {
	    var self = this;
	    this.a.push(s);
	    return this;
	};
	this.pop = function () {
	    if (this.a.length == 0) return null;
	    var s = this.a[this.a.length - 1];
	    this.a.pop();
	    return s;
	};
	this.remove = function (expression) {
	    if (!isNaN(expression)) {
	        this.a = [].concat(this.a.slice(0, expression), this.a.slice(expression + 1));
	        return this.a;
	    } else {
	        for (var i = 0; i < this.a.length; i++) {
	            if (expression(this.a[i])) {
	                return this.remove(i);
	            }
	        }
	    }
	    return this.a;
	};
	this.get = function (index) {
	    if (this.a.length-1 <index) return null;
	    return this.a[index];
	};
	this.exists = function (expression) {
	    if (expression == null) return false;
	    for (var i = 0; i < this.a.length; i++) {
	        if (expression(this.a[i])) {
	            return i;
	        }
	    }
	    return false;
	};
	this.getTop = function () {
	    if (this.a.length==0) return null;
	    return this.a[this.a.length-1];
	};
	this.getIndex = function (expression) {
	    if (expression == null) return null;
	    for (var i = 0; i < this.a.length; i++) {
	        if (expression(this.a[i])) {
	            return i;
	        }
	    }
	    return null;
	};
	this.first = function (expression) {
	    if (expression == null) return null;
	    for (var i = 0; i < this.a.length; i++) {
	        if (expression(this.a[i])) {
	            return this.a[i];
	        }
	    }
	    return null;
	};
	this.swap = function (index1, index2) {
	    var temp = this.a[index1];
	    this.a[index1] = this.a[index2];
	    this.a[index2] = temp;
	};
	this.moveToTop = function (index) {
	    if (isNaN(index)) return;
	    if (this.a.length - 1 < index) return;
	    var o = this.get(index);
	    this.remove(index);
	    this.push(o);
	};
	this.length = function () {
	    return this.a.length;
	}
}
window.laytime = 0;
(function ($) {
    $.fn.layout = function (fn) {
        if (fn == null) {
            return this.each(function () {
            window.laytime++;
                $(this).trigger("layout");
                //var _this=$(this);
                $(this).find(".layoutable").trigger("layout");

            });
        } else {
            return this.each(function () {
                $(this).attr("layoutable", "1");
                $(this).addClass("layoutable");
                $(this).on("layout", fn);
            });
        }
    };
    
    $.fn.renderui = function () {
        var _this=$(this);
        _this.find("a[target],area[target]").link({application:App});
        _this.find(".datetime").each(function() {
            var $this = $(this);
            var opts = {};
			if ($this.attr("dateFmt")) opts.pattern = $this.attr("dateFmt");
			if ($this.attr("minDate")) opts.minDate = $this.attr("minDate");
			if ($this.attr("maxDate")) opts.maxDate = $this.attr("maxDate");
			if ($this.attr("mmStep")) opts.mmStep = $this.attr("mmStep");
			if ($this.attr("ssStep")) opts.ssStep = $this.attr("ssStep");
            $this.datepicker(opts);
        });
        _this.find(".verticalSplitContainer").verticalSplitContainer();
        _this.find(".horizontalSplitContainer").horizontalSplitContainer();
        _this.find(".listpage").listpage();
        _this.find(".addpage").addpage();
        _this.find(".checkboxCtrl").selectAllToggle({target:"[name='ids']"});
        _this.find(".storechooser").storechooser({application:App});
        _this.find(".reportsubmit").reportsubmit({application:App});
        _this.find(".numberinput").numberinput();
        _this.find(".intinput").intinput();
        //auto bind tabs
	    _this.find("div.tabs").each(function(){
		    var $this = $(this);
		    var options = {};
		    options.currentIndex = $this.attr("currentIndex") || 0;
		    options.eventType = $this.attr("eventType") || "click";
		    $this.tabs(options);
	    });


        
        
       
        
		    _this.find("textarea.editor").each(function(){
			    var $this = $(this);
			    var op = {html5Upload:false, skin: 'vista',tools: $this.attr("tools") || 'full'};
			    var upAttrs = [
				    ["upLinkUrl","upLinkExt","zip,rar,txt"],
				    ["upImgUrl","upImgExt","jpg,jpeg,gif,png"],
				    ["upFlashUrl","upFlashExt","swf"],
				    ["upMediaUrl","upMediaExt","avi"]
			    ];
			
			    $(upAttrs).each(function(i){
				    var urlAttr = upAttrs[i][0];
				    var extAttr = upAttrs[i][1];
				
				    if ($this.attr(urlAttr)) {
					    op[urlAttr] = $this.attr(urlAttr);
					    op[extAttr] = $this.attr(extAttr) || upAttrs[i][2];
				    }
			    });
			
			    $this.xheditor(op);
		    });


	  



    };
    $.fn.remote = function (fn) {
        if (fn == null) {
            return this.each(function () {
                $(this).trigger("remote");
            });
        } else {
            return this.each(function () {
                $(this).on("remote", fn);
            });
        }
    };
    $.fn.helper = function (name,fn) {
        if (fn != null) {
            this.data("helper_"+name, fn);
        } else {
            return this.data("helper_"+name);
        }
    };
    $.fn.PanelParent=function(){
        return this.closest("[panelid],[dialogid]");
    };
    $.fn.CloseContainer=function(){
        if(this.closest("[dialogid]").size()>0){
            this.closest("[dialogid]").helper("dialog").close();
        }else if(this.closest("[panelid]").size()>0){
            var panelid=this.closest("[panelid]").attr("panelid");
            App.getTabPanelContainer().removeTab(panelid);
        }
    };
    $.setRegional = function(key, value){
		if (!$.regional) $.regional = {};
		$.regional[key] = value;
	};
    $.fn.numberinput=function(){
        return this.each(function () {
                $(this).css("ime-mode","disabled");
                $(this).keyup(function(){
                    $(this).val($(this).val().replace(/[^\d\.\-]/g,''));
                });
        });
    };
    $.fn.intinput=function(){
        return this.each(function () {
                $(this).css("ime-mode","disabled");
                $(this).keyup(function(){
                    $(this).val($(this).val().replace(/[^\d]/g,''));
                });
        });
    };
    $.fn.disabledinput = function() {
        return this.each(function() {
            if ($(this).attr("readonly") == "readonly") {
                $(this).css({ background: "#eeeeee" });
            } else {
                $(this).css({ background: "#ffffff" });
            }
        });
    };
})(jQuery);